package com.zlsy.learning.leetcode.sort;

import java.util.Arrays;

/**
 * @author zhouliang
 * @date 2020/8/24 20:31
 *     <p>选择排序演示
 */
public class SelectionSort {

  public static void main(String[] args) {
    int[] a = new int[] {4, 5, 6, 3, 2, 1};
    int n = a.length;
    int[] ints = selectionSort(a, n);
    System.out.println(Arrays.toString(ints));
  }

  private static int[] selectionSort(int[] a, int n) {
    if (n <= 1) {
      return a;
    }
    for (int i = 0; i < n - 1; i++) {
      // 查找最小值
      int minIndex = i;
      for (int j = i + 1; j < n; j++) {
        if (a[j] < a[minIndex]) {
          minIndex = j;
        }
      }
      // 交换
      int temp = a[i];
      a[i] = a[minIndex];
      a[minIndex] = temp;
    }
    return a;
  }
}
